local desc_v_2v2 = [[
  # 联动时刻简介

  游戏由两名先发队和两名后发队进行，胜利目标为使所有敌方角色离场。

  座位排列为 先发-后发-后发-先发。

  一名角色离场后，其队友会摸一张牌。

  第一回合角色摸牌阶段少摸一张牌。四号位多摸一张初始手牌。

  队友手牌可见。

  过劳机制：

  结束阶段，若真实轮次>16，你流失一点体力。

  【请期待后续的特殊交互。】
]]

local v_m_2v2_getLogic = function()
  local v_m_2v2_logic = GameLogic:subclass("v_m_2v2_logic")

  function v_m_2v2_logic:assignRoles()
    local room = self.room
    local n = #room.players
    local roles =
      { "v_m_2v2_first", "v_m_2v2_second", "v_m_2v2_second", "v_m_2v2_first" }

    for i = 1, n do
      local p = room.players[i]
      p.role = roles[i]
      p.role_shown = true
      room:broadcastProperty(p, "role")
    end

    room.players[1]:addBuddy(room.players[4])
    room.players[4]:addBuddy(room.players[1])
    room.players[2]:addBuddy(room.players[3])
    room.players[3]:addBuddy(room.players[2])

    self.start_role = roles[1]
    -- for adjustSeats
    room.players[1].role = "lord"
  end

  function v_m_2v2_logic:chooseGenerals()
  --   测试用，之后给他安装上副将机制。
  --   local room = self.room
  --   local generalNum = room.settings.generalNum
  --   local n = room.settings.enableDeputy and 2 or 1

  --   local lord = room:getLord()
  --   room.current = lord
  --   lord.role = self.start_role

  --   local nonlord = room.players
  --   local generals = Fk:getGeneralsRandomly(#nonlord * generalNum)
  --   table.shuffle(generals)
  --   for _, p in ipairs(nonlord) do
  --     local arg = {}
  --     for i = 1, generalNum do
  --       if #generals > 0 then
  --         table.insert(arg, table.remove(generals, 1).name)
  --       end
  --     end
  --     p.request_data = json.encode{ arg, n }
  --     p.default_reply = table.random(arg, n)
  --   end

  --   room:doBroadcastRequest("AskForGeneral", nonlord)
  --   for _, p in ipairs(nonlord) do
  --     local mainGeneral, deputyGeneral
  --     if p.general == "" and p.reply_ready then
  --       local _generals = json.decode(p.client_reply)
  --       mainGeneral = _generals[1]
  --       deputyGeneral = _generals[2]
  --     else
  --       mainGeneral = p.default_reply[1]
  --       deputyGeneral = p.default_reply[2]
  --     end
  --     room:setPlayerGeneral(p, mainGeneral, true, true)
  --     room:setDeputyGeneral(p, deputyGeneral)
  --     table.removeOne(room.general_pile, mainGeneral)
  --     table.removeOne(room.general_pile, deputyGeneral)
  --     p.default_reply = ""
  --   end

  --   room:askForChooseKingdom(nonlord)
  --   room:setTag("SkipNormalDeathProcess", true)
  -- end

  --非测试，正式版用
    local room = self.room
    local generalNum = math.min(room.settings.generalNum, 9)

    local lord = room:getLord()
    room.current = lord
    lord.role = self.start_role

    local nonlord = room.players
    local generals = table.map(Fk:getGeneralsRandomly(#nonlord * generalNum), Util.NameMapper)
    table.shuffle(generals)
    local t1 = table.slice(generals, 1, generalNum + 1)
    local t2 = table.slice(generals, generalNum + 1, generalNum * 2 + 1)
    local t3 = table.slice(generals, generalNum * 2 + 1, generalNum * 3 + 1)
    local t4 = table.slice(generals, generalNum * 3 + 1, generalNum * 4 + 1)
    room:askForMiniGame(nonlord, "AskForGeneral", "v_2v2_sel", {
      [nonlord[1].id] = {
        friend_id = nonlord[4].id,
        me = t1, friend = t4,
      },
      [nonlord[2].id] = {
        friend_id = nonlord[3].id,
        me = t2, friend = t3,
      },
      [nonlord[3].id] = {
        friend_id = nonlord[2].id,
        me = t3, friend = t2,
      },
      [nonlord[4].id] = {
        friend_id = nonlord[1].id,
        me = t4, friend = t1,
      },
    })

    for _, p in ipairs(nonlord) do
      local general = json.decode(p.client_reply)
      room:setPlayerGeneral(p, general, true, true)
    end

    --这段代码别碰！
    --[[
    for _, p in ipairs(nonlord) do
      local arg = {}
      for i = 1, generalNum do
        table.insert(arg, table.remove(generals, 1).name)
      end
      p.request_data = json.encode({ arg, 1 })
      p.default_reply = arg[1]
    end

    room:doBroadcastRequest("AskForGeneral", nonlord)
    for _, p in ipairs(nonlord) do
      if p.general == "" and p.reply_ready then
        local general = json.decode(p.client_reply)[1]
        room:setPlayerGeneral(p, general, true, true)
      else
        room:setPlayerGeneral(p, p.default_reply, true, true)
      end
      p.default_reply = ""
    end
    --]]

    -- 这段要用的时候你还是需要反注释的
    room:askForChooseKingdom(nonlord)
    room:setTag("SkipNormalDeathProcess", true)
  end

  return v_m_2v2_logic
end

local v_m_xieli = fk.CreateActiveSkill{
  name = "v_m_xieli",
  anim_type = "support",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return 
      (not player:isKongcheng())
      and player:usedSkillTimes(self.name, Player.HistoryPhase) < 1
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and Fk:currentRoom():getPlayerById(to_select).role == Self.role
  end,
  on_use = function(self, room, effect)
    -- local target = room:getPlayerById(effect.tos[1])
    local cid = effect.cards[1]
    -- room:obtainCard(target, effect.cards[1], true, fk.ReasonGive)
    if type(cid) ~= "number" then
      assert(cid and cid:isInstanceOf(Card))
      cid = cid:isVirtual() and cid.subcards or {cid.id}
    else
      cid = {cid}
    end
    if #cid == 0 then return end
  
    -- if type(target) == "table" then
    --   target = target.id
    -- end
    room:moveCards({
      ids = cid,
      from = room.owner_map[cid[1]],
      to = effect.tos[1],
      toArea = Card.PlayerHand,
      moveReason = fk.ReasonGive,
      proposer = effect.tos[1],
      skillName = self.name,
      moveVisible = true,
    })
  end,
}
Fk:addSkill(v_m_xieli)

local v_m_2v2_rule = fk.CreateTriggerSkill{
  name = "#v_m_2v2_rule",
  priority = 0.001,
  refresh_events = {fk.DrawInitialCards, fk.DrawNCards, 
    fk.GameStart, 
    -- fk.RoundStart, fk.RoundEnd, 
    fk.GameOverJudge, fk.Deathed,
    fk.EventPhaseEnd},
  can_refresh = function(self, event, target, player, data)
    if event == fk.GameStart then
      return true
    else
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DrawNCards then
      if player.seat == 1 and player:getMark(self.name) == 0 then
        room:addPlayerMark(player, self.name, 1)
        room:setTag("SkipNormalDeathProcess", true)
        data.n = data.n - 1
      end
    elseif event == fk.DrawInitialCards then
      if player.seat == 4 then
        data.num = data.num + 1
      end
    elseif event == fk.GameStart then
      -- for _,p in ipairs(room:getAlivePlayers()) do
      --   room:handleAddLoseSkills(p, "v_m_xieli", nil, false)
      -- end
      room:handleAddLoseSkills(player, "v_m_xieli", nil, false)
    -- elseif event == fk.RoundStart then
    --   room:setTag("trueround_rule2_used", true)
    --   for _, p in ipairs(room:getAllPlayers()) do
    --     --这里后续可以加入特殊疲劳机制判定（例如秋凛子）
    --     if p:getMark("@@v_exhaustion") == 0 and p:getMark("v_noexhaustion") == 0 then
    --       room:setPlayerMark(p, "@@v_exhaustion", 1)
    --     elseif p:getMark("@@v_noexhaustion") == 0 and p:getMark("v_noexhaustion") > 0 then
    --       room:setPlayerMark(p, "@@v_noexhaustion", 1)
    --     end
    --   end
    -- elseif event == fk.RoundEnd then
    --   room:setTag("trueround_rule2_used", false)
    elseif event == fk.GameOverJudge then
      local winner = Fk.game_modes[room.settings.gameMode]:getWinner(player)
      if winner ~= "" then
        room:gameOver(winner)
        return true
      else
        for _, p in ipairs(room.alive_players) do
          if p.role == player.role then
            p:drawCards(1)
          end
        end
      end
    -- elseif event == fk.EventPhaseEnd then
    --   if player.phase == Player.Finish then
    --     if player:getMark("@@v_exhaustion") > 0 and player:isAlive() then
    --       --这里后续可以加入特殊疲劳机制判定（例如秋凛子）
    --       --这里后续可以加入每回合增加的弹幕/战报文案
    --       if player:getMark("v_phoenix") > 0 then
    --         room:drawCards(player, 2, self.name)
    --         room:damage{
    --           to = player,
    --           damage = 1,
    --           damageType = fk.FireDamage,
    --           skillName = self.name,
    --         }
    --       else
    --         room:loseHp(player, 1)
    --       end
    --     end
    --   end
    end
  end,
}
Fk:addSkill(v_m_2v2_rule)

local v_m_2v2_mode = fk.CreateGameMode{
  name = "v_m_2v2_mode",
  minPlayer = 4,
  maxPlayer = 4,
  rule = v_m_2v2_rule,
  logic = v_m_2v2_getLogic,
  surrender_func = function(self, playedTime)
    local surrenderJudge = { { text = "time limitation: 2 min", passed = playedTime >= 120 },
    { text = "v_m_2v2: left you alive", passed = table.find(Fk:currentRoom().players, function(p)
      return p.role == Self.role and p.dead
    end) and true } }
    return surrenderJudge
  end,
  winner_getter = function(self, victim)
    local room = victim.room
    local alive = table.filter(room.alive_players, function(p)
      return not p.surrendered
    end)
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end,
}
Fk:loadTranslationTable{
  [":v_m_2v2_mode"] = desc_v_2v2,
}

Fk:addMiniGame{
  name = "v_2v2_sel",
  qml_path = "packages/VslTest/qml/gamemode/v_m_2v2",
  default_choice = function(player, data)
    return data.me[1]
  end,
  update_func = function(player, data)
    local room = player.room
    local d = player.mini_game_data.data
    local friend = room:getPlayerById(d.friend_id)
    friend:doNotify("UpdateMiniGame", json.encode(data))
  end,
}

-- 加载本包的翻译包(load translations of this package)，这一步在本文档的最后进行。
-- dofile "packages/vupslash/i18n/init.lua"
dofile "packages/VslTest/i18n/init.lua"

return v_m_2v2_mode
